import React from "react";
import { getDocBaseUrl } from "../../../../utils/common";

export const CustomLink = (props: React.AnchorHTMLAttributes<HTMLAnchorElement>) => {
    const { href, children } = props;
    const baseUrl = getDocBaseUrl();
    if (/^[0-9]+$/.test(href)) {
        // 文档路径使用文档id为数字
        return <a {...props} href={`${baseUrl}/${href}`}>{children}</a>;
    }
    if (/^https?:\/\//.test(href)) {
        return <a {...props}/>;
    }
    return (
        <a {...props} href={`${baseUrl}/${href}`}>{children}</a>
    );
};

export const CustomLinkPreview = (props: React.AnchorHTMLAttributes<HTMLAnchorElement>) => {
    const { href, children } = props;
    if (/^[0-9]+$/.test(href)) {
        // 文档路径使用文档id为数字
        return <a {...props} href={`?id=${href}`}>{children}</a>;
    }
    return <a {...props}/>;
};